package com.yang.service.adminimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.bean.MarketGoods;
import com.yang.bean.MarketTopic;
import com.yang.bean.MarketTopicExample;
import com.yang.bean.common.BasePageInfo;
import com.yang.bean.common.BaseRespVo;
import com.yang.bean.common.CommonData;
import com.yang.bean.szy.BatchDeleteBO;
import com.yang.bean.szy.QueryTopicVo;
import com.yang.mapper.MarketGoodsMapper;
import com.yang.mapper.MarketTopicMapper;
import com.yang.service.admin.AdminTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AdminTopicServiceImpl implements AdminTopicService {
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public BaseRespVo<CommonData<MarketTopic>> queryAllTopic(BasePageInfo info, String title, String subtitle) {
        PageHelper.startPage(info.getPage(),info.getLimit());
        MarketTopicExample example = new MarketTopicExample();
        example.setOrderByClause(info.getSort() + " "+ info.getOrder());
        MarketTopicExample.Criteria criteria=example.createCriteria();
        if (title !=null && !"".equals(title)){
            criteria.andTitleLike("%"+title+"%");
        }
        if (subtitle !=null && !"".equals(subtitle)){
            criteria.andSubtitleLike("%"+subtitle+"%");
        }
        //内层
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExampleWithBLOBs(example);
        //中层
        CommonData<MarketTopic> marketTopicCommonData = new CommonData<>();
        marketTopicCommonData.setList(marketTopics);

        //外层
        return BaseRespVo.ok(marketTopicCommonData);
    }

    @Override
    public BaseRespVo<MarketTopic> createTopic(MarketTopic marketTopic) {
        MarketTopic marketTopic1 = new MarketTopic();
        marketTopic1.setId(marketTopic.getId());
        marketTopic1.setTitle(marketTopic.getTitle());
        marketTopic1.setSubtitle(marketTopic.getSubtitle());
        marketTopic1.setPrice(marketTopic.getPrice());
        marketTopic1.setGoods(marketTopic.getGoods());
        marketTopic1.setAddTime(new Date());
        marketTopic1.setUpdateTime(new Date());
        marketTopic1.setContent(marketTopic.getContent());
        marketTopicMapper.insert(marketTopic);
        return BaseRespVo.ok(marketTopic1);

    }

    @Override
    public QueryTopicVo queryTopic(Integer id) {
        QueryTopicVo data = new QueryTopicVo();
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        data.setTopic(marketTopic);
        String[] goods = marketTopic.getGoods();
        ArrayList<MarketGoods> marketGoodsArrayList = new ArrayList<>();
        for (String good : goods) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(Integer.valueOf(good));
            marketGoodsArrayList.add(marketGoods);
        }
        data.setGoodsList(marketGoodsArrayList);
        return data;
    }

    @Override
    public BaseRespVo<MarketTopic> updateTopic(MarketTopic marketTopic) {
        marketTopicMapper.updateByPrimaryKey(marketTopic);

        return BaseRespVo.ok(marketTopic);
    }

    @Override
    public void deleteTopic(MarketTopic marketTopic) {
        marketTopicMapper.deleteByPrimaryKey(marketTopic.getId());
    }

    @Override
    public void deleteTopics(BatchDeleteBO batchDeleteBO) {
        List<String> ids = batchDeleteBO.getIds();
        for (String id : ids) {
            marketTopicMapper.deleteByPrimaryKey(Integer.valueOf(id));
        }
    }

    @Override
    public BaseRespVo<CommonData<MarketTopic>> relatedTopic(Integer id) {
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        ArrayList<MarketTopic> marketTopics = new ArrayList<>();
        marketTopics.add(marketTopic);
        CommonData<MarketTopic> marketTopicCommonData = new CommonData<>();
        marketTopicCommonData.setList(marketTopics);
        return BaseRespVo.ok(marketTopicCommonData);

    }

    @Override
    public CommonData list(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(marketTopicExample);

        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(marketTopics);
        CommonData data = CommonData.data(marketTopicPageInfo);
        return data;
    }


}
